<?php
namespace Admin\Controller;
use Think\Controller;
class PyqBannerController extends IndexController{

       /*
         首页展示模板
         并处理 添加  修改两方法
         type==1  添加一条信息
         type==2  修改一条信息
     */
    public function lists(){
        $this->display();
        $type=I('request.type');
        if($type==1){
            $Banner=D("Pyq_banner");
            $data['content_title']=I("request.content_title");
            $data['content']= I("request.content");
            $data['orders']=I("request.orders");
            $data['create_time']=time();
            if($_FILES['image']['name']!=null){
                header("content-type:text/html;charset=utf-8");
                $upload = new \Think\Upload(); // 实例化上传类
                $upload->maxSize = 1024 * 1024;// 设置附件上传大小 (-1) 是不限值大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = "./Public/imageBANNER/";
                $upload->replace = true; //存在同名文件是否是覆盖
                $upload->autoSub = true;  // 是否使用子目录保存上传文件
                $upload->saveName = time() . '_' . mt_rand();   // 采用date函数生成命名规则 传入Y-m-d参数
                $info = $upload->uploadOne($_FILES['image']);
                if (!$info) {
                    $data['image'] = '';
                } else {
                    $data['image'] = ltrim($info['savepath'] . $info['savename'], '/');
                }
            }

            $Banner->add($data);
            $this->redirect('PyqBanner/lists');
        }
        if($type==2){
            $this->actionUpdate();

        }


    }

    /*获取首页展示信息列表
     @params  string  iDisplayStart  dataTable 自带的参数
     @params array $result 查询成功返回的结果
     return false 0 || result result
    */
    public function get_lists(){
        $Banner=D("Pyq_banner");
        if(I("request.iDisplayStart")==''){

            $start=0;
        }else{
            $start=I("request.iDisplayStart");
        }

        $result=$Banner->limit($start,I("request.iDisplayLength"))->select();
        if($result){

            $ret = array(
                "sEcho" => intval($_GET['sEcho']),
                "iTotalRecords" => $start,
                "iTotalDisplayRecords" => $Banner->count(),
                "aaData" =>$result
            );
            echo json_encode($ret);exit;

        }else{
            echo json_encode(0);
        }
    }

    /*
       用于添加信息的模板
    */
    public function add(){

        $this->display();
    }


    /*
      用于修改信息的模板
   */
    public function update(){

        $id= I("request.id");
        setcookie('uid',$id,time()+3600);
        $this->display();
    }

    /*
      获取一条需要修改的数据
   */
    public function update_list(){
        $id= $_COOKIE['uid'];
        $Banner=D("Pyq_banner");
        $map['id']= $id;
        $result=$Banner->where($map)->find();
        if($result){
            echo json_encode($result); exit;
        }else{
            echo json_encode(0); exit;
        }

    }

    public function actionUpdate(){
        $id= $_COOKIE['uid'];
        $Banner=D("Pyq_banner");
        $map['id']=$id;
        $data['content_title']= I("request.content_title");
        $data['content']= I("request.content");
        $data['orders']= I("request.orders");
        if($_FILES['image']['name']!=null){
            header("content-type:text/html;charset=utf-8");

            $image=$Banner->where($map)->getField("image"); //查询图片地址用于删除图片
            $upload = new \Think\Upload(); // 实例化上传类
            $upload->maxSize = 1024 * 1024;// 设置附件上传大小 (-1) 是不限值大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = "./Public/imageBANNER/";
            $upload->replace = true; //存在同名文件是否是覆盖
            $upload->autoSub = true;  // 是否使用子目录保存上传文件
            $upload->saveName = time() . '_' . mt_rand();   // 采用date函数生成命名规则 传入Y-m-d参数
            $info = $upload->uploadOne($_FILES['image']);
            if (!$info) {

                $data['image'] = '';
            } else {
                unlink("./Public/imageBANNER/".$image);
                $data['image'] = ltrim($info['savepath'] . $info['savename'], '/');
            }
        }

        $Banner->where($map)->save($data);
        $this->redirect('PyqBanner/lists');
    }

    public function del(){
        $id= I("request.id");
        if($id!=""){
            $Banner=D("Pyq_banner");
            $map['id']=$id;
            $image=$Banner->where($map)->getField('image');
            if($image!=""){
                unlink("./Public/imageBANNER/".$image);
            }
            $result=$Banner->where($map)->delete();
            if($result){
                echo json_encode(1);exit;
            }
        }
    }

}